<?php
class HostAction extends BaseAction {
	function index(){
		$idx = isset($_GET['idx'])?$_GET['idx']:0;
		$host=new HostModel();
		$keyword=$_POST['keyword'];
		$ftype=$_POST['ftype'];
		if(!empty($keyword) && !empty($ftype)){
			$where[$ftype]=array('like',$keyword.'%');
			$_SESSION['keyword']=$where;
		}else{
			if(empty($keyword) && !empty($ftype)){
				unset($_SESSION['keyword']);
			}else if(!empty($_SESSION['keyword'])){
				$where=$_SESSION['keyword'];
			}
		}
		import('ORG.Util.Page');
		$count=$host->where($where)->count();
		$page=new Page($count,C('PAGESIZE'));
		$show=$page->show();
		$this->assign("page",$show);
		//需要查询哪些字段
        $fields = 'id,name,domain,ip';
		$list=$host->order('id')->field($fields)->where($where)->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('list',$list);
		//功能模块按钮
		$option = get_option($idx);
		$this->assign('coption',$option);
		$this->display();
	}

	function insert(){
		$host=new HostModel();
		if($data=$host->create()){
			if(false!==$host->add()){
				/*---------------操作日志-----START----------*/
				import("@.Util.Slog");
				Slog::getInstance()->write(CRUD_C,$data);
				/*---------------操作日志-----END------------*/
				$autoId=$host->getLastInsID();
				$this->assign('jumpUrl',__URL__.'/index');
				$this->success('操作成功，插入数据编号为：'.$autoId);
			}else{
				$this->error('操作失败：addHost'.$host->getDbError());
			}
		}else{
			$this->error('操作失败：数据验证( '.$host->getError().' )');
		}
	}

	function update(){
		$host=new HostModel();
		if($data=$host->create()){
			if(!empty($data['id'])){
				if(false!==$host->save()){
					/*---------------操作日志-----START----------*/
					import("@.Util.Slog");
					Slog::getInstance()->write(CRUD_U,$data);
					/*---------------操作日志-----END------------*/
					$this->assign('jumpUrl',__URL__.'/index');
					$this->success('操作成功');
				}else{
					$this->error('操作失败：'.$host->getDbError());
				}
			}else{
				$this->error('请选择编辑用户');
			}
		}else{
			$this->error('操作失败：数据验证( '.$host->getError().' )');
		}
	}


	function edit(){
		$id=$_GET['id'];
		if(!empty($id)){
			$host=new HostModel();
			$date=$host->field('id,name,domain,ip,dbname,dbuser,dbpwd,dbpoint,dest')->getById($id);
			$this->assign('obj',$date);
			//按钮权限
			$option = $_SESSION['soption'];
			$this->assign('coption',$option);
		}
		$this->display();
	}

	function delete(){
		$did=$_POST['did'];
		if(!empty($did) && is_array($did)){
			$host=new HostModel();
			$id=implode(',',$did);
			if(false!==$host->delete($id)){
				/*---------------操作日志-----START----------*/
				import("@.Util.Slog");
				Slog::getInstance()->write(CRUD_D,$did);
				/*---------------操作日志-----END------------*/
				$this->assign('jumpUrl',__URL__.'/index');
				$this->success('操作成功');
			}else{
				$this->error('操作失败：'.$host->getDbError());
			}
		}else{
			$this->error('请选择删除用户');
		}
	}
}
?>